package study.kit.el;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

@Slf4j
public class TableV2Utils {

    static Map<String, String> MAPPING = MapUtil.<String, String>builder()
            .put("季度", "quarter")
            .put("课次", "lessonNo")
            .put("课程名称", "lessonName")
            .put("知识点", "knoPoint")
            .put("切片链接", "cut")
            .put("难度", "difficulty")
            .put("首三推荐", "f3")
            .put("重要性（文本说明）", "importance")
            .put("题库知识图谱", "knoAtlas")
            .build();

    @Test
    public void test() {
        //
        String json = ResourceUtil.readStr("zh/table.json", Charset.defaultCharset());

        // 标题
        Map<Integer, String> headMap = buildHeadMap(json);
        //log.info("{}", headMap);

        // 数据行
        JSONArray rows = readRows(json);
        log.info("{}", rows);
        List<TableVO> VOLt = ListUtil.toList();
        for (int i = 0; i < 4; i++) {
            // 数据列
            JSONArray cols = JsonPath.read(rows, StrUtil.format("$.[{}][2]", i));
            log.info(">>>>>> {}", cols.size());
            TableVO VO = new TableVO();
            for (int j = 0; j < cols.size(); j++) {
                // 序号
                Integer index = JsonPath.read(cols, StrUtil.format("$.[{}].[0]", j));
                // 值
                Object value = JsonPath.read(cols, StrUtil.format("$.[{}].[1].value", j));
                String name = MapUtil.getStr(headMap, index);
                //log.info("{}, {}= {}", index, name, value);
                fillBean(VO, index, value, headMap);
            }

            VOLt.add(VO);
        }
        log.info("{}", VOLt);
    }

    /**
     * 读取
     */
    private JSONArray readRows(String jsonStr) {
        if (StrUtil.isEmpty(jsonStr)) {
            return null;
        }
        return JsonPath.read(jsonStr, "$.content.s1.rows[1:5]");
    }

    private JSONArray readCols(JSONArray rows, int colsNum) {
        return JsonPath.read(rows, StrUtil.format("$.[{}][2]", colsNum));
    }

    private Integer readIndex(String jsonStr) {
        return JsonPath.read(jsonStr, "$.content.s1.rows[1:5]");
    }

    private Object readValue(String jsonStr) {
        return JsonPath.read(jsonStr, "$.content.s1.rows[1:5]");
    }

    // 构造
    private Map<Integer, String> buildHeadMap(String json) {
        JSONArray headCols = JsonPath.read(json, "$.content.s1.rows[0][2]");
        return CollectionUtil.toMap(headCols, null,
                e -> JsonPath.read(e, "$.[0]"),
                e -> JsonPath.read(e, "$.[1].value"));
    }

    // 填充
    private void fillBean(TableVO VO, int index, Object value, Map<Integer, String> headMap) {
        //
        String headName = MapUtil.getStr(headMap, index);
        String fname = MapUtil.getStr(MAPPING, headName);
        if (StrUtil.isEmpty(fname)) {
            return;
        }
        ReflectUtil.setFieldValue(VO, fname, value);
    }
}
